package com.midea.ai.air.view;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ouyangdi on 2016/4/18.
 */
public class LoadingDialog {
    public static final int DIALOG_DISMISS = 1;
    private Context mContext;
    private Dialog mDialog;
    private ImageView mIvCircle; //圆形进度
    private TextView mTvMsg;     //显示文本

    private String mLoadingText = "";
    private String mPosMsg = "";
    private String mNegMsg = "";
    ObjectAnimator mAnimator;
    private Timer mTimer;

    class TaskDismiss extends TimerTask {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(DIALOG_DISMISS);
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == DIALOG_DISMISS) {
                if(mDialog.isShowing()) {
                    mDialog.dismiss();
                }
                if(mTimer!= null) {
                    mTimer.cancel();
                    mTimer.purge();
                    mTimer = null;
                }
            }
        }
    };

    public LoadingDialog(Context context) {
        mContext = context;
    }

    public LoadingDialog builder() {
        //获取Dialog布局
        View view = View.inflate(mContext, R.layout.myloadingdialog, null);

        //获取自定义Dialog布局中的控件
        mIvCircle = (ImageView) view.findViewById(R.id.loadingImg);
        mTvMsg = (TextView) view.findViewById(R.id.loadingmsg);

        //定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.my_loading_dialog);
        mDialog.setContentView(view);
        return this;
    }

    public void setLoadingText(String loadingText) {
        mLoadingText = loadingText;
        mTvMsg.setText(mLoadingText);
    }

    public void setPosMsg(String posMsg) {
        mPosMsg = posMsg;
    }

    public void setNegMsg(String negMsg) {
        mNegMsg = negMsg;
    }

    public void show() {
        mTvMsg.setText(mLoadingText);
        mDialog.show();
        startProgressAnim();
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    /**
     * 对话框消失之后的处理
     * @param listener 监听事件
     */
    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        mDialog.setOnDismissListener(listener);
    }
    /**
     * 开始旋转动画
     */
    public void startProgressAnim() {
        mAnimator = ObjectAnimator.ofFloat(mIvCircle, "rotation", 0.0f, 360.0f);
        mAnimator.setDuration(1200);
        mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.start();
    }

    /**
     * 停止旋转动画并显示执行结果
     */
    public void stopProgressAnim(boolean isSuccess) {
        mAnimator.end();
        if(isSuccess) {
            mIvCircle.setImageResource(R.drawable.air_loading_success);
            mTvMsg.setText(mPosMsg);
        } else {
            mIvCircle.setImageResource(R.drawable.air_loading_failure);
            mTvMsg.setText(mNegMsg);
        }

        mTimer = new Timer();
        mTimer.schedule(new TaskDismiss(), 500);
    }

    /**
     * 停止旋转动画并关闭Loading框
     */
    public void stopProgressAnim() {
        mAnimator.end();
        if(mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
